<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/3/2
 * Time: 11:42
 */

namespace App\Http\Logics\Admin;

use App\TrainContent;
use App\Http\Logics\BaseLogic;
class TrainContentLogic extends BaseLogic
{
    public static function updateTrainContent($data)
    {
        // 将之前的删除
        TrainContent::where('train_id' ,$data['train_id'])->delete();

        $content = json_decode($data['content_str']);
        foreach ($content as $key => $item){
            $train_content = new TrainContent();
            // 公众参数
            $train_content->train_id = $data['train_id'];
            $train_content->sort = $key;
            $train_content->type = $item->type;

            switch ($item->type){
                case TrainContent::TEXT:
                    $train_content->text = $item->text;
                    if (!$train_content->save()){
                        return self::response(self::OBJECT_SAVE_FAIL);
                    }
                    break;
                case TrainContent::PHOTO:
                    $train_content->photo_url = $item->photo_url;
                    $url = $item->photo_url . "?imageInfo";
                    // 处理图片
                    $rs = dealHttp($url);
                    $width = $rs->width;
                    $height = $rs->height;
                    $train_content->photo_width = $width;
                    $train_content->photo_height = $height;
                    if (!$train_content->save()){
                        return self::response(self::OBJECT_SAVE_FAIL);
                    }
                    break;
                case TrainContent::VIDEO:
                    $train_content->video_url = $item->video_url;
                    $train_content->video_cover = $item->video_cover;
                    $train_content->video_width = $item->video_width;
                    $train_content->video_height = $item->video_height;
                    if (!$train_content->save()){
                        return self::response(self::OBJECT_SAVE_FAIL);
                    }
                    break;
                case TrainContent::ADDRESS:
                    $train_content->address = $item->address;
                    $train_content->address_name = $item->address_name;
                    $train_content->lat = $item->lat;
                    $train_content->lng = $item->lng;
                    if (!$train_content->save()){
                        return self::response(self::OBJECT_SAVE_FAIL);
                    }
                    break;
                default:
                    break;
            }
        }
        return self::response(self::SUCCESS);
    }

    public static function getTrainContentDetail($data)
    {
        $train_content = TrainContent::where(function ($query) use ($data){
            $query->where('train_id', $data['train_id']);
        })->orderBy('sort')->get();
        if (empty($train_content)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $train_content);
    }
}